﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TimKen.Web.Admin.Inbox
{
    public partial class Inbox_Add : TimKen.App.AdminPage
    {
        TimKen.BussLogic.InboxManager inboxM = new TimKen.BussLogic.InboxManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["MsgID"] != null)
                {
                    MsgID = Request.QueryString["MsgID"];
                    BindMsg();
                }
                BindResources();
            }
        }

        public override string ActionCode
        {
            get
            {
                return "95";
            }
        }

        protected void BindMsg()
        {
            if (MsgID != "")
            {
                TimKen.DataEntity.Inbox msg = inboxM.GetModel(int.Parse(MsgID));
                if (msg == null)
                {
                    Response.Redirect("/ErrorAuthority.aspx");
                }
                else if (msg.ToID != CurrentUserName && msg.FromID != CurrentUserName)
                {
                    Response.Redirect("/ErrorAuthority.aspx");
                }
                else
                {
                    if (!msg.IsRead)
                    {
                        msg.IsRead = true;
                        inboxM.Update(msg);
                    }
                    txtToID.Text = msg.FromID;
                    txtTitle.Text = GetLangString("ReplyMessage") + "：" + msg.Title;
                    txtMessage.Text = "\n\n\n----------------------------------------------------\n";
                    txtMessage.Text += ">>" + GetLangString("MessageFrom") + "：" + msg.FromID + "\n";
                    txtMessage.Text += ">>" + GetLangString("MessageTitle") + "：" + msg.Title + "\n";
                    txtMessage.Text += ">>" + GetLangString("MessageCreateDate") + "：" + msg.CreateDate.Value.ToShortDateString() + "\n";
                    txtMessage.Text += msg.Message + "\n";
                    txtMessage.Text += "----------------------------------------------------\n";
                }
            }
        }

        protected string MsgID
        {
            get
            {
                if (ViewState["MsgID"] != null)
                    return (ViewState["MsgID"].ToString());
                else
                    return "";
            }
            set
            {
                ViewState["MsgID"] = value;
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            TimKen.DataEntity.Inbox msg = new TimKen.DataEntity.Inbox();
            msg.FromID = CurrentUserName;
            msg.Message = txtMessage.Text;
            msg.Title = txtTitle.Text;
            msg.ToAll = chkToAll.Checked ? 1 : 0;
            msg.ToID = txtToID.Text;
            inboxM.Add(msg);
            Response.Redirect("Inbox_List.aspx");
        }
        protected void BindResources()
        {
            btnSubmit.Text = GetLangString("Save");
            chkToAll.Text = GetLangString("MessageToAll");
        }

        protected void chkToAll_CheckedChanged(object sender, EventArgs e)
        {
            txtToID.ReadOnly = chkToAll.Checked;
        }
    }
}
